/**
 * 
 */
package ar.com.greensoft.neosystem.dao.jpa;

import java.io.Serializable;

import javax.persistence.TypedQuery;

import ar.com.greensoft.neosystem.dao.PersonaDao;
import ar.com.greensoft.neosystem.model.Persona;
import ar.com.greensoft.neosystem.model.references.TipoDocumento;

/**
 * 
 * @author Jorge E. Villaverde
 * @since 24/01/2012
 * @version 1.0.0
 */
public abstract class JpaPersonaDao<T extends Persona, Dto extends Serializable> 
	extends AbstractLongIdJpaDaoDtoImpl<T, Dto> 
	implements PersonaDao<T> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public T findByCuit(String cuit) {
		if(logger.isTraceEnabled())
			logger.trace("Returning entity of type: " + getEntityClazz().getSimpleName() + "\twith cuit: " + cuit);
		
		TypedQuery<T> query = getEntityManager().createNamedQuery(
				getFindByCuitQueryName(), getEntityClazz());
		
		query.setParameter("cuit", cuit);
		
		try {
			return query.getSingleResult();
		} catch (javax.persistence.NoResultException e) {
		}
		return null;
	}

	@Override
	public T findByTipoNroDocumento(TipoDocumento tipoDocumento, Long numeroDocumento) {
		
		TypedQuery<T> query = getEntityManager().createNamedQuery(
				getFindByTipoNroDocumentoQueryName(), getEntityClazz());
		
		query.setParameter("tipoDocumento", tipoDocumento);
		query.setParameter("numeroDocumento", numeroDocumento);
		
		try {
			return query.getSingleResult();
		} catch (javax.persistence.NoResultException e) {
		}
		return null;
	}
	
	protected abstract String getFindByCuitQueryName();

	protected abstract String getFindByTipoNroDocumentoQueryName();
}
